package com.woniu.cq.medical.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.cq.medical.common.PageResult;
import com.woniu.cq.medical.common.Result;
import com.woniu.cq.medical.controller.form.PatientForm;
import com.woniu.cq.medical.entity.Patient;
import com.woniu.cq.medical.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-09-05
 */
@RestController
@RequestMapping("/medical/patient")
@CrossOrigin("*")//解决跨域问题
@Slf4j
public class PatientController {
    @Autowired
    private PatientService patientService;

    /**
     * 根据身份证查询用户信息
     * @return
     */
    @RequestMapping("selectPatientByIdCard")
    public Result selectPatientByIdCard(String idCard){
        log.info("传入的参数-身份证为：{}",idCard);
        //根据身份证查询患者基本信息
        Patient patient = patientService.selectPatientByIdCard(idCard);
        log.info("查询出的患者基本信息为：{}",patient);
        return Result.success(patient);
    }

    /**
     * 查询所有用户信息
     * @param pageIdx
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    public IPage<Patient> selectAll(@RequestParam(defaultValue = "1") int pageIdx,
                                    @RequestParam(defaultValue = "10") int pageSize,String name){
        log.info("传入的参数-页数：{}",pageIdx);

        IPage<Patient> pageResult =  patientService.selectAll(pageIdx,pageSize,name);
        log.info("返回的对象：{}",pageResult);
        return pageResult;
    }

    @RequestMapping("/updateById")
    public Result updateByIdPatient(String patientAge,String patientSex,String patientId,String patientName,String patientAddress,String patientOccupation,String patientPhoneNumber){
        log.info("传入的参数-病人id：{}",patientId);
        Patient patient=new Patient();
        patient.setPatientId(Integer.parseInt(patientId));
        patient.setPatientName(patientName);
        patient.setPatientAddress(patientAddress);
        patient.setPatientOccupation(patientOccupation);
        patient.setPatientPhoneNumber(patientPhoneNumber);
        patient.setPatientLastModificationTime(new Date());
        patient.setPatientSex(patientSex);
        patient.setPatientAge(Integer.parseInt(patientAge));
        patientService.updateByIdPatient(patient);

        return Result.success();
    }

    @RequestMapping("addpatient")
    public Result addPatient(@RequestBody @Valid PatientForm patientForm){
        Patient patient=new Patient();
        BeanUtils.copyProperties(patientForm,patient);
        patient.setPatientPassword("123");
        patient.setPatientIntoTime(new Date());
        patient.setPatientLastModificationTime(new Date());
        patientService.addPatient(patient);
        return Result.success();
    }

}

